package com.peas.fastdfs.config;


import lombok.Data;
import org.springframework.amqp.core.AcknowledgeMode;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.boot.autoconfigure.amqp.SimpleRabbitListenerContainerFactoryConfigurer;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConfigurationProperties(prefix = "spring.rabbitmq.local")
@Data
public class RabbitmqConfig {

    private String host;
    private Integer port;
    private String username;
    private String passowrd;
    private String virtualhost;



    @Bean
    public ConnectionFactory localConnectionFactory(){
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
        connectionFactory.setHost(host);
        connectionFactory.setPort(port);
        connectionFactory.setUsername(username);
        connectionFactory.setPassword(passowrd);
        connectionFactory.setVirtualHost(virtualhost);
        connectionFactory.setPublisherConfirms(true);
        connectionFactory.setPublisherReturns(true);
        return connectionFactory;
    }


    @Bean
    public SimpleRabbitListenerContainerFactory localSimpleRabbitListenerContainerFactory(SimpleRabbitListenerContainerFactoryConfigurer configurer, ConnectionFactory localConnectionFactory){
        SimpleRabbitListenerContainerFactory simpleRabbitListenerContainerFactory = new SimpleRabbitListenerContainerFactory();
        simpleRabbitListenerContainerFactory.setAcknowledgeMode(AcknowledgeMode.MANUAL);
        simpleRabbitListenerContainerFactory.setConcurrentConsumers(1);
        simpleRabbitListenerContainerFactory.setMaxConcurrentConsumers(3);
        configurer.configure(simpleRabbitListenerContainerFactory,localConnectionFactory);
        return simpleRabbitListenerContainerFactory;
    }



    @Bean
    public RabbitTemplate localRabbitTemplate(ConnectionFactory localConnectionFactory){
        RabbitTemplate rabbitTemplate = new RabbitTemplate(localConnectionFactory);
        rabbitTemplate.setMandatory(true);
        return rabbitTemplate;
    }
}
